package com.flipdf2.serialize.ppml.element;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;
import org.jdom.Namespace;

import com.flipdf2.serialize.ppml.PPMLException;

public class PPMLReusableObject {
	public static final String NAME = "REUSABLE_OBJECT";
	
	private List<PPMLObject> objectList;
	private PPMLView view;
	private PPMLOccurrenceList occurrenceList;
	
	public Element toXML(Namespace defaultNamespace) {
		Element root = null;
		if (defaultNamespace != null) {
			root = new Element(NAME, defaultNamespace);
		} else {
			root = new Element(NAME);	
		}
		
		if (objectList != null && objectList.size() > 0) {
			for (int i = 0, n = objectList.size(); i < n; i++) {
				root.addContent(objectList.get(i).toXML(defaultNamespace));
			}
		}
		
		if (view != null)
			root.addContent(view.toXML(defaultNamespace));
		
		if (occurrenceList != null)
			root.addContent(occurrenceList.toXML(defaultNamespace));
		
		return root;
	}

	public void check() throws PPMLException {
		if (objectList == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ELEMENT_LOST, "OBJECT");
		}
		if (occurrenceList == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ELEMENT_LOST, "OCCURRENCE_LIST");
		}
	}
	
	public List<PPMLObject> getObjectList() {
		return objectList;
	}

	public void setObjectList(List<PPMLObject> objectList) {
		this.objectList = objectList;
	}
	
	public void addObject(PPMLObject object) {
		if (this.objectList == null)
			this.objectList = new ArrayList<PPMLObject>();
		this.objectList.add(object);
	}

	public PPMLOccurrenceList getOccurrenceList() {
		return occurrenceList;
	}

	public void setOccurrenceList(PPMLOccurrenceList occurrenceList) {
		this.occurrenceList = occurrenceList;
	}

	public PPMLView getView() {
		return view;
	}

	public void setView(PPMLView view) {
		this.view = view;
	}
	
}
